<?php
class news_model extends  CI_Model
{
	function __construct(){
		 parent::__construct();

	} 
	
	function getNumNews()
	{
		$this->db->where('app',0);
		$query = $this->db->get('e_news');
		return $query->num_rows(); 
	}
	
	function getAllNews($num=20, $offset=0)
	{
		$this->db->where('active',1);
		$this->db->order_by('content','DESC');
		$query = $this->db->get('e_news',$num, $offset);
		return $query->result();
	}
	
	function savenews()
	{
		 $id = (int)$this->uri->segment(3);
		 $data = array(
			'title_vn' => (string)$this->input->post('ContentTitle'),
			'alias' => strtolower(stringchange($this->input->post('ContentTitle'))),
			'image' => (string)$this->input->post('ContentImg'),
			'contensmall' => (string)$this->input->post('ContentSmall'),
			'content' => (string)$this->input->post('ContentBody'),
			'active' => (int)$this->input->post('IsActive'),
			'app' => 0,
			'date' => time()
		 );
		 if($id!=0){
			  $this->db->where('news_id',$id);
			  if($this->db->update('e_news',$data)){
					return true;
			  }else{
					return false;
			  }
		 }else{
			  if($this->db->insert('e_news',$data)){
					return true;
			  }else{
					return false;
			  }
		 }
	}

	function getItemNews($id)
	{
		$this->db->where('news_id',$id);
		$query = $this->db->get('e_news');	
		return $query->row();
	}

	function delete($id)
	{
		$this->db->where('news_id',$id);
		if($this->db->delete('e_news'))
		{
			return true;
		}else{
			return false;
		}
	}
	
	
}
?>